home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7262 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: pegasus.montclair.edu!harmon
  2. From: harmon@pegasus.montclair.edu (Derek Harmon)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: volatile type
  5. Date: 14 Feb 1996 02:37:03 -0500
  6. Organization: Montclair State University
  7. Message-ID: <harmon.824282241@pegasus.montclair.edu>
  8. References: <4fro3j$jbi@service.polymtl.ca>
  9. NNTP-Posting-Host: pegasus.montclair.edu
  10. X-Newsreader: NN version 6.5.0 #68 (NOV)
  11.  
  12. fermi@info.polymtl.ca (Yannick Heneault) writes:
  13.  
  14. >Can someone know information about this data type. A example is
  15. >welcome!
  16.  
  17.    volatile isn't a data type, its a modifier to a data declaration, much
  18. like const, static, or extern communicate additional information about a
  19. particular data declaration to the compiler.  In the case of the volatile
  20. keyword, it means that this element of data could be changed at any time
  21. by any means, including means outside the control of your own program
  22. (for example, other processes or interrupts).  For this reason, the
  23. compiler is informed that it should always update its value of that
  24. element from memory, and not rely on more optimized handling that would
  25. place it in a register and assume that if it hasn't been referenced in
  26. the program, then it hasn't changed.  volatile means that it could have
  27. changed, and there is no way for the compiler to know when or how, it
  28. must always fetch it when called upon.
  29.  
  30. ex.
  31.      volatile int widgets;
  32.      :
  33.      widgets = 0;
  34.      :
  35.      while ( widgets < 10 ) {
  36.         printf("not enough widgets\n");
  37.      }
  38.  
  39.    In this example, widgets are initialized to 0.  We will assume somewhere
  40. that everytime a clock interrupt occurs, or an assembly line worker pulls a
  41. chord signifying a new widget has been produced, that widgets is modified by
  42. an external program (interrupt service routine, parent process, whatever)
  43. and incremented.
  44.  
  45.    Any reasonable compiler would see that the while loop is infinite, and as
  46. widgets is not changed within the loop, there would ordinarily be no reason
  47. for the compiler to generate code that, at the while condition, would check
  48. widgets against 10.  After all, a shortened jump and tighter loop would
  49. ordinarily be considered an optimization, it would be more efficient.  BUT,
  50. in this case we know that widgets could exceed 10 during the loop.  Therefore
  51. widgets is declared as volatile.  This forces the compiler to test the con-
  52. dition by comparing widgets to 10, and more so, it prevents the compiler from
  53. copying the value of widgets from out in memory into a high-speed register
  54. (since if widgets were updated since that copy were made, the register would
  55. not likewise be updated), instead it must consult the memory location for
  56. widgets.
  57.  
  58.                                                   -- Stone
  59. --
  60. # Derek Harmon (aka Stonelight)    harmon@pegasus.montclair.edu
  61. # - Computer Science Undergrad, Montclair State University, NJ
  62. # - My views are my own, nobody else is this creative.  3;)>
  63. ... CYCLIC REDUNDANCY CHECK - stocktaking at a bicycle shop.
  64.  
  65.